// arrList.js
import { distribute } from "./distribute";
import { insertZerosByControl } from "./insert";

// 将 n 和 bases 提取为参数传递，而不是在文件内硬编码
function generateArrListFromBases(n, bases) {
    // 检查 bases 是否为有效的数组
    if (!Array.isArray(bases)) {
        console.error("Invalid bases: bases should be an array.");
        return []; // 返回空数组，避免后续错误
    }
    return bases.map(base => [n, base]);
}

function batchGenerateRowcases(n, bases) {
    const arrList = generateArrListFromBases(n, bases); // 生成 arrList
    
    // 如果 arrList 为空，直接返回空数组
    if (arrList.length === 0) {
        console.error("arrList is empty. Unable to proceed.");
        return [];
    }

    const result = [];

    arrList.forEach(([n, base]) => {
        // 确保 base 是有效的数组
        if (!Array.isArray(base)) {
            console.error("Invalid base: base should be an array.");
            result.push([]); // 跳过无效 base
            return;
        }

        const sum = base.reduce((a, b) => a + b, 0);
        const totalGaps = base.length - 1;
        const totalZeros = n - (sum + totalGaps);
        const numSlots = base.length + 1;

        if (totalZeros < 0) {
            // 无法填充，跳过
            result.push([]);
            return;
        }

        const controls = distribute(totalZeros, numSlots);
        const rowcases = controls.map(ctrl => insertZerosByControl(base, ctrl));

        result.push(rowcases);
    });

    return result;
}

export default { batchGenerateRowcases };







